package org.lzuploader.gui.listeners.main;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Shell;
import org.lzuploader.gui.WMain;
import org.lzuploader.gui.helpers.EasyMessageBox;
import org.lzuploader.model.Config;
import org.lzuploader.model.Localization;
import org.lzuploader.model.account.Account;
import org.lzuploader.model.account.AccountServerConfig;

/**
 * This class is used when user clicks on the "save password" checkbox.
 * 
 * @author Emmanuel Cron
 * 
 */
public class SavePasswordSelectionListener implements SelectionListener
{
    /**
     * Logger of this class.
     */
    private static Log log = LogFactory.getLog(SavePasswordSelectionListener.class);
    
    /**
     * Localization of this class.
     */
    private Localization localization = Localization.getInstance();

    /**
     * Used when widget is selected by default.
     * 
     * @param e
     *            Associated event
     */
    @Override
    public void widgetDefaultSelected(final SelectionEvent e)
    {
        widgetSelected(e);
    }

    /**
     * Used when widget is manually selected.
     * 
     * @param e
     *            Associated event
     */
    @Override
    public void widgetSelected(final SelectionEvent e)
    {
        log.debug("User clicked on save password button.");

        Shell wMainShell = null;
        for (int i = 0; i < e.display.getShells().length; i++)
        {
            if (e.display.getShells()[i].getData() instanceof WMain)
            {
                wMainShell = e.display.getShells()[i];
                break;
            }
        }

        final WMain main = (WMain) wMainShell.getData();

        final Config config = Config.getInstance();
        final Account account = config.getAccounts().get(main.getAccountsList().getText());
        final String server = main.getServersList().getText();
        final AccountServerConfig accountServerConfig = account.getServerConfigs().get(server);
        final boolean isSaveable = main.getSavePasswordCheckbox().getSelection();
        
        log.trace("Manually checking save password checkbox in order to display message box (if needed).");
        
        main.getSavePasswordCheckbox().setSelection(true);
        
        if (
            isSaveable == false &&
            accountServerConfig.getUsername() != null &&
            accountServerConfig.getPassword() != null &&
            EasyMessageBox.yesno(
                localization.getString("DIALOG_TITLE_YES_NO"),
                localization.getString("DIALOG_UNCHECKING_WILL_RESET")
            ) == SWT.NO
        )
        {
            return;
        }
        
        if (isSaveable == false)
        {
            main.getSavePasswordCheckbox().setSelection(false);
            
            log.trace("Reseting username/pwd to null.");
            
            accountServerConfig.setUsername(null);
            accountServerConfig.setPassword(null);
        }
        
        log.trace("Setting saveable password to '" + isSaveable + "' for account " + account.getName() + " on server " + server);
        
        accountServerConfig.setAuthentificationSaveable(isSaveable);
        
        // Saving config
        config.save();
    }
}
